package service

import (
	"errors"
	"net/http"
	"robot/cache"
	"robot/database"
	"robot/util"
)

func Login(w http.ResponseWriter, uid int, password string) (bool, error) {
	var pass string
	err := database.Db.QueryRow("SELECT * FROM admin_user WHERE id = ?", uid).Scan(&pass)
	if err != nil {
		return false, err
	}
	if pass == password {
		session := util.RandString(10)
		cache.Session.SetLoginSession(uid, session)

		return true, nil
	}
	return false, errors.New("password error")
}

func CheckPassword(uid int, password string) (bool, error) {
	var pass string
	err := database.Db.QueryRow("SELECT * FROM admin_user WHERE id = ?", uid).Scan(&pass)
	if err != nil {
		return false, err
	}
	if pass == password {

		return true, nil
	}
	return false, errors.New("password error")
}
